package org.six.background.controller;

import org.six.background.mapper.InshopMapper;
import org.six.background.mapper.ShopMapper;
import org.six.background.mapper.ShoptypeMapper;
import org.six.background.pojo.Inshop;
import org.six.background.pojo.Shop;
import org.six.background.pojo.Shoptype;
import org.six.background.service.ShopService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@ResponseBody
@RequestMapping("/shop")
public class ShopController {
    @Autowired
    private ShopService shopService;
    @Autowired(required = false)
    private ShopMapper shopMapper;
    @Autowired(required = false)
    private InshopMapper inshopMapper;
    @Autowired(required = false)
    private ShoptypeMapper shoptypeMapper;

    //查
    @RequestMapping("/show")
    public List<Shop> shopshow(){
        return shopService.shopshow();
    }

    @RequestMapping("/upload")
    public String upload(@RequestParam("file") MultipartFile file,HttpServletRequest req){
        return shopService.upload(file,req);
    }
    //增
    @RequestMapping("/add")
    public String shopadd(Shop shop){
        int i = shopService.shopadd(shop);
        if(i>0&&i<2){
            return "ok";
        }else {
            return "no";
        }
    }

    //删
    @RequestMapping("/delete")
    public String shopdelete(Integer shopid){
        System.out.println(shopid);
        return shopService.shopdelete(shopid);
    }

    //改
    @RequestMapping("/updata")
    public int shoprevise(Shop shop){

        return shopService.shoprevise(shop);

    }
    @RequestMapping("/list")
    public Map list(){
        List<Shop> shops = shopMapper.selectByExample(null);

        List<Shoptype> shoptypes = shoptypeMapper.selectByExample(null);
        Map map=new HashMap();
        map.put("shops",shops);
        map.put("shoptypes",shoptypes);

        System.out.println(map.toString());
        return map;
    }

    @RequestMapping("/inshop")
    public Map inlist(){
        List<Inshop> inshops = inshopMapper.selectByExample(null);
        Map map=new HashMap();
        map.put("inshops",inshops);

        System.out.println(map.toString());
        return map;
    }
}
